package edu.gatech.cs2340.thecyclonesquad;

import java.util.Date;

import edu.gatech.cs2340.thecyclonesquad.database.Database;
import edu.gatech.cs2340.thecyclonesquad.models.InMemoryModel;
import edu.gatech.cs2340.thecyclonesquad.models.Item;
import edu.gatech.cs2340.thecyclonesquad.models.Person;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class AddLostItemActivity extends Activity implements OnItemSelectedListener {
	
	InMemoryModel lostitems = new InMemoryModel();
	Spinner spinner2;
	String[] categories = { "Category: Keepsakes", "Category: Heirlooms" , "Category: Misc" };
	String category;
	boolean status = false;
	
	
	@SuppressLint("NewApi")
	@Override
	/**
	 * Initializes our activity.
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_lost_item);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddLostItemActivity.this, android.R.layout.simple_spinner_item, categories);
		spinner2 = (Spinner) findViewById(R.id.spin2);
		spinner2.setAdapter(adapter);
		spinner2.setOnItemSelectedListener(this);
		
		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
	}
	
	@Override
	/**
	 * Initialize the contents of the Activity's standard options menu.
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_lost_item, menu);
		return true;
	}
	
	/**
	 * Assigns values to category depending on spinner selection.
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		//gets position of one selected
		int position = spinner2.getSelectedItemPosition();
		switch(position) {
			case 0:
				category = "Keepsakes"; 
				break;
			case 1:
				category = "Heirlooms";
				break;
			case 2:
				category = "Misc";
				break;
		}
		
	}

	/**
	 * Method for if nothing selected in spinner.
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Assigns status a value if checked or not.
	 * @param view is the basic building block for UI components
	 */
	public void onCheckboxClicked(View view) {
		// Is the view now checked?
		boolean checked = ((CheckBox) view).isChecked();
		if (checked) {
			status = true;
		}
	}
	
	/**
	 * Adds new lost item to database.
	 * @param view is the basic building block for UI components
	 */
	public void addNewLostItem(View view) {
		EditText editText = (EditText) findViewById(R.id.name);
		String name = editText.getText().toString();
		EditText editText2 = (EditText) findViewById(R.id.description);
		String description = editText2.getText().toString();
		EditText editText3 = (EditText) findViewById(R.id.location);
		String location = editText3.getText().toString();
		EditText editText4 = (EditText) findViewById(R.id.reward);
		String reward = editText4.getText().toString();
	//	EditText editText5 = (EditText) findViewById(R.id.date);
		//String date = editText5.getText().toString();
		//java.text.DateFormat date = java.text.DateFormat.getDateTimeInstance();
		Date date = new Date();
		
		String owner = InMemoryModel.currentUser;
		String type = "Lost";
		//needs name
		if (!name.equals("") && !location.equals("")) {
			Database entry = new Database(this);
	    	entry.open();
	    	boolean exists = entry.itemThere(owner, name, type);
	    	if (!exists) {
	    		entry.createItem(owner, name, description, location, reward, date, type, category, status);
	    	}	
	    	entry.close();
			
			Intent intent = new Intent(this, LostItemsActivity.class);
			startActivity(intent);
		}
	}
	
	/**
	 * Goes back to previous page if decide to cancel.
	 * @param view is the basic building block for UI components
	 */
	public void cancelAddLost(View view) {
		Intent intent = new Intent(this, LostItemsActivity.class);
		startActivity(intent);
	}
	
	/**
	 * logOut method logs the user out and goes back to login page.
	 * @param view is the basic building block for UI components
	 */
	public void logOut(View view) {
		Intent intent = new Intent(this, SignInActivity.class);
		startActivity(intent);			
	}
}
